package start.redisstream;

import cn.hutool.core.math.MathUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.stream.StreamListener;
import start.util.SpringContextUtil;

import java.util.Map;

@Slf4j
//@Component
public class ProductUpdateStreamListener02 implements StreamListener<String, MapRecord<String, String, String>> {

    private String customerName;

    private String group;

    public ProductUpdateStreamListener02(String customerName, String group) {
        this.customerName = customerName;
        this.group = group;
    }

    @Override
    public void onMessage(MapRecord<String, String, String> message) {

        StringRedisTemplate redisTemplate = (StringRedisTemplate) SpringContextUtil.getBean("stringRedisTemplate");
        RecordId recordId = message.getId();
        String stream = message.getStream();
        Map<String, String> record = message.getValue();
        log.info("[手动] customer{} group:[{}] 接收到一个消息 stream:[{}],id:[{}],value:[{}]", customerName, group, stream, recordId, record);
        // 消息处理完毕后，确认该消息，以便从PEL中删除该消息ID
        redisTemplate.opsForStream().acknowledge(group, message);
        redisTemplate.opsForStream().delete(message);
    }
}
